<template>
  <div class="teacher-system">
    <!-- 系统头部 -->
    <header class="system-header">
      <h1>教师管理系统</h1>
      <div class="user-info">
        <span>欢迎，{{ username }}</span>
        <button @click="logout">退出登录</button>
      </div>
    </header>

    <!-- 功能导航 -->
    <nav class="system-nav">
      <ul>
        <li @click="activeTab = 'course'" :class="{ active: activeTab === 'course' }">课程管理</li>
        <li @click="activeTab = 'grade'" :class="{ active: activeTab === 'grade' }">成绩录入</li>
        <li @click="activeTab = 'notice'" :class="{ active: activeTab === 'notice' }">通知发布</li>
      </ul>
    </nav>

    <!-- 内容区域 -->
    <main class="system-content">
      <!-- 课程管理模块 -->
      <div v-show="activeTab === 'course'" class="course-management">
        <div class="course-actions">
          <el-button type="primary" @click="showCreateDialog">新建课程</el-button>
          <el-button type="warning" @click="exportGrades">导出成绩</el-button>
        </div>
        <el-dialog v-model="createDialogVisible" title="新建课程">
          <el-form :model="newCourse">
            <el-form-item label="课程名称">
              <el-input v-model="newCourse.name" />
            </el-form-item>
            <el-form-item label="课程代码">
              <el-input v-model="newCourse.code" />
            </el-form-item>
          </el-form>
          <template #footer>
            <el-button @click="createDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="createCourse">确认</el-button>
          </template>
        </el-dialog>
        <h2>我的课程</h2>
        <div class="course-list">
          <div class="course-card" v-for="course in courses" :key="course.id">
            <h3>{{ course.name }}</h3>
            <p>课程代码：{{ course.code }}</p>
            <p>学生人数：{{ course.students }}人</p>
          </div>
        </div>
      </div>

      <!-- 成绩录入模块 -->
      <div v-show="activeTab === 'grade'" class="grade-input">
        <h2>成绩录入</h2>
        <div class="grade-table">
          <table>
            <thead>
              <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>成绩</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="student in students" :key="student.id">
                <td>{{ student.id }}</td>
                <td>{{ student.name }}</td>
                <td>
                  <input type="number" v-model.number="student.grade" min="0" max="100" />
                </td>
                <td>
                  <button @click="saveGrade(student)">保存</button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>

      <!-- 通知发布模块 -->
      <div v-show="activeTab === 'notice'" class="notice-publish">
        <h2>发布通知</h2>
        <div class="notice-form">
          <el-input v-model="noticeTitle" placeholder="通知标题" />
          <el-input type="textarea" v-model="noticeContent" placeholder="通知内容" rows="4" />
          <el-button @click="publishNotice">发布通知</el-button>
        </div>
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const authStore = useAuthStore()
const username = ref(authStore.currentUser?.name || '未知用户')
const router = useRouter()

// 导航状态
const activeTab = ref('course')

// 课程数据
const courses = ref([
  { id: 1, name: 'Web前端开发', code: 'CS101', students: 45 },
  { id: 2, name: '数据库原理', code: 'CS102', students: 38 },
  { id: 3, name: '软件工程', code: 'CS103', students: 42 }
])

// 学生成绩数据
const students = ref([
  { id: '2023001', name: '张三', grade: 85 },
  { id: '2023002', name: '李四', grade: 92 },
  { id: '2023003', name: '王五', grade: 78 }
])

// 通知数据
const noticeTitle = ref('')
const noticeContent = ref('')

const createDialogVisible = ref(false)
const newCourse = ref({
  name: '',
  code: '',
  students: 0
})

const createCourse = () => {
  if (!newCourse.value.name || !newCourse.value.code) {
    alert('请填写完整的课程信息')
    return
  }
  courses.value.push({
    ...newCourse.value,
    id: Date.now()
  })
  createDialogVisible.value = false
  newCourse.value = {
    name: '',
    code: '',
    students: 0
  }
}

const exportGrades = () => {
  // 导出成绩逻辑
  alert('成绩导出成功')
}

// 保存成绩
const saveGrade = (student) => {
  if (student.grade < 0 || student.grade > 100) {
    alert('成绩范围应在0-100之间')
    return
  }
  console.log('保存成绩：', student)
  alert(`已保存 ${student.name} 的成绩：${student.grade}`)
}

// 发布通知
const publishNotice = () => {
  if (!noticeTitle.value || !noticeContent.value) {
    alert('请填写完整的通知信息')
    return
  }
  console.log('发布通知：', {
    title: noticeTitle.value,
    content: noticeContent.value
  })
  alert('通知发布成功！')
  noticeTitle.value = ''
  noticeContent.value = ''
}

// 退出登录
const logout = () => {
  authStore.logout()
  router.push('/login')
}

// 初始化数据
onMounted(() => {
  // 这里可以添加初始化数据请求
})
</script>

<style scoped>
.teacher-system {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.system-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background: #f5f7fa;
  border-radius: 8px;
  margin-bottom: 20px;
}

.user-info button {
  padding: 8px 16px;
  background: #409eff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.system-nav ul {
  display: flex;
  gap: 20px;
  padding: 0;
  list-style: none;
}

.system-nav li {
  padding: 10px 20px;
  cursor: pointer;
  border-radius: 4px;
  transition: background 0.3s;
}

.system-nav li.active {
  background: #409eff;
  color: white;
}

.course-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}

.course-card {
  padding: 20px;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  transition: box-shadow 0.3s;
}

.course-card:hover {
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.grade-table table {
  width: 100%;
  border-collapse: collapse;
}

.grade-table th,
.grade-table td {
  padding: 12px;
  border: 1px solid #ebeef5;
  text-align: left;
}

.grade-table input {
  width: 80px;
  padding: 6px;
}

.notice-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
</style>